package WordFrequent;

import java.io.File;
import java.io.IOException;
import java.util.*;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import javafx.beans.binding.MapExpression;
import org.apache.commons.io.*;

public class Zerostep {


    static  private  int [] wordsnumber = new int[27];



    //将文件内容传输至字符串然后计算大小写字母的个数。
    public static int count(String words){
        int num = 0;
          words = words.toLowerCase();
            for (int i = 0;i<words.length();i++){
                if ( (words.charAt( i ) > 64 && words.charAt( i ) < 91) || (words.charAt( i ) > 96 && words.charAt( i ) < 122) ){
                            num++;
                            }
                }

        return num;

    }

        //计算每个字母出现的次数
     public static void Wordsnumber(String words){

        for (int i = 0 ;i < words.length() ; i++){
                if (words.charAt(i) > 96 && words.charAt(i) < 123){
                    wordsnumber[words.charAt(i)-97]++;
                }
            }

     }



        public static void  sort(int num){
            Map<Character, Integer> map = new TreeMap<Character, Integer>();
            //构建比较器comparator假如A的值大于B，你返回1，此时调用Collections.sort()函数就是升序；假如A的值大于B，你返回-1，此时调用Collections.sort()函数就是降序。


            Comparator<Map.Entry<Character,Integer>> com = new Comparator<Map.Entry<Character, Integer>>() {
                @Override
                public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
                    //先按字母出现的次数进行比较， 后者间前者降序排序
                    if (o2.getValue() != o1.getValue())return o2.getValue() - o1.getValue();
                        //若字母出现的次数相同则按字母的ASCII码进行比较，前者减后者升序排序，这一步其实可以不用因为wordNumber就是按字母的顺序排好的
                    else return o1.getKey() - o2.getKey();
                }
            };
            for (int i = 0; i < 26 ; i++){
                map.put((char)(i+97),wordsnumber[i]);
            }


            List<Map.Entry<Character, Integer>> list = new ArrayList<Map.Entry<Character, Integer>>( map.entrySet());
            Collections.sort( list,com );

            //将words中的字母和次数当做键值对传入map。



            for (int i = 0; i<list.size();i++){
                double rate =  ((list).get(i).getValue()*1.0)/(num*1.0) * 100 ;
                System.out.printf( "%c \t \t%d\t \t%.2f%%\n",list.get(i).getKey(),list.get(i).getValue(),rate,"%");

            }
        }
}
